(1) What is PEAR in PHP?
PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries. It also provides a command line interface to install “packages” automatically.
(2) Sorting in PHP
Below are different sorting methods –
function customSort($a, $b) {
return $a - $b; // Sort in ascending order
}
$myArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
usort($myArray, "customSort");
print_r($myArray);
- sort(): Sort by value in ascending order.
- rsort(): Sort by value in decending order.
- ksort(): Sort by key in ascending order.
- krsort(): Sort by key in decending order.
- asort(): Sort by value in ascending order.
- arsort(): Sort by value in decending order.
- usort(): Sort value with custom comparison function
function customSort($a, $b) {
return $a - $b; // Sort in ascending order
}
$myArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
usort($myArray, "customSort");
print_r($myArray);
(3) What is concrete classes and methods?
In PHP, concrete classes and methods function similarly to other object-oriented languages. A concrete class is one that can be instantiated and all of its methods are defined. PHP, just like Java or C#, uses classes and objects as the fundamental building blocks for creating complex data structures, such as linked lists.
Here is an example of a PHP concrete class for a node in a linked list and a method to reverse a linked list:
Here is an example of a PHP concrete class for a node in a linked list and a method to reverse a linked list:
class ListNode {
public $val;
public $next;
public function __construct($val) {
$this->val = $val;
$this->next = null;
}
// Reverse the linked list
public static function reverse($head) {
$prev = null;
$current = $head;
$next = null;
while ($current !== null) {
$next = $current->next; // temporarily store the next node
$current->next = $prev; // reverse the current node's pointer
$prev = $current; // move pointers one position forward
$current = $next;
}
return $prev; // prev is the new head of the reversed list
}
}
// Usage:
// Create a linked list: 1 -> 2 -> 3 -> 4 -> null
$head = new ListNode(1);
$head->next = new ListNode(2);
$head->next->next = new ListNode(3);
$head->next->next->next = new ListNode(4);
// Reverse the linked list
$reversedHead = ListNode::reverse($head);
// Print the reversed linked list
$current = $reversedHead;
while ($current !== null) {
echo $current->val . ' ';
$current = $current->next;
}
// Output should be: 4 3 2 1
In this PHP example:
ListNode
is a concrete class as it can be instantiated and contains an implementation for the constructor and the staticreverse
method.- The
reverse
static method takes the head of a linked list as its argument and returns the new head of the list after reversal.
The usage section of the script creates a simple linked list with the values 1 through 4, reverses the list, and then prints out the values of the reversed list. The output of the provided PHP code should display the numbers in reverse order.